//##include('../block/lazyload.js');
//##include('../block/doT.js');
//##include('../block/zeptotouch.js');

var index,
    str,
    type,
    id,
    pagesize,
    $coverlist,
    winH,
    $word,
    iT
    ,iH,
    nT,
    distance,
    touchMove,
    pos,
    mainTop,
    mainHeight;

var _view = {
    init: function () {
        this.ajaxRequest();
        this.shortAction();
    },
    ajaxRequest: function () {
        $.ajax({
            type: 'GET',
            url: '/pedia/pedia_apis/coverlist?type=' + type + '&id=' + id + "&p=" + pagesize,
            dataType: 'json',
            success: function (data) {
                var len = data.list.length;
                if (len > 0) {
                    if (len == 1) {
                        $(".introduce_content").css("display", "block");
                        $("#introduce .head ").off("tap");
                        $("#introduce .head span:nth-of-type(2) ").hide();
                    }
                    var temID = $coverlist.attr('temId');
                    var tmpl = $("#" + temID).html();
                    var doTtmpl = doT.template(tmpl);
                    $coverlist.append(doTtmpl(data));
                    $(".lazy").lazyload({
                        effect: "fadeIn"
                    });
                } else {
                    if (pagesize == 1) {
                        $("#newest").hide();
                        $(".introduce_content").css("display", "block");
                        $("#introduce .head ").off("tap");
                        $("#introduce .head span:nth-of-type(2) ").hide();

                    } else {
                        if ($("#newest").css("display") == "none") {
                            $("#nomore").removeClass("block");
                        }
                        $("#nomore").addClass("block");
                    }
                }
            },
            error: function () {
                console.log('Ajax error!');
            }
        });
    },
    shortAction: function () {
        $word.addClass("action");
    }


};
var _event = {
    binding: function () {
        $("#bigP").on("tap", this.wordToggle);
        $("#introduce .head").on("tap", this.clickIntroduce);
        $("#main-container").bind({"touchstart":this.start,"touchmove":this.move,"touchend":this.end})

    },
    wordToggle: function () {
        $word.toggle();
    },
    start: function (e) {
        this.touchStart = e.changedTouches[0].pageY;
        this.offsetT = $(this).offset().top;
    },
    move: function (e) {
        e.preventDefault();
        e.stopPropagation();
        this.flag = true;
        touchMove = e.changedTouches[0].pageY;
        pos = touchMove - this.touchStart;
        mainTop = $(this).offset().top;
        mainHeight = $(this).height();
        iT = $("#introduce .head").offset().top;
        iH = $("#introduce .head").height();
        nT = $("#newest").offset().top;
        distance=nT - iH;

        if (pos > 0) {//down
            $(this).offset({top: this.offsetT + pos});
            if (mainTop >= 0) {
                $(this).offset({top: 0});
            }

        } else if (pos < 0) {//up
            if (mainHeight <= winH) {
                $(this).offset({top: 0});
            } else {
                $(this).offset({top: this.offsetT + pos});
                if (-mainTop >= mainHeight - winH) {
                    $(this).offset({top: -(mainHeight - winH)})
                }
            }


        }
        if (!($("#introduce .introduce_content").height() == 0)) {//展开
            if ($("#introduce").offset().top <= 0) {
                $("#introduce .wasHide").css("display", "block");
                $("#introduce .head").addClass("fixed");
            } else {
                $("#introduce .wasHide").css("display", "none");
                $("#introduce .head").removeClass("fixed");
            }
            if ($("#newest").offset().top <= 0) {
                $("#newest .wasHide").css("display", "block");
                $("#newest h2").addClass("fixed");
            } else {
                $("#newest .wasHide").css("display", "none");
                $("#newest h2").removeClass("fixed");
            }

            if (distance <= 0) {

                $("#introduce .head").css("margin-top", distance);
            } else {
                $("#introduce .head").css("margin-top", 0);
            }
            if ($("#newest").height() == 0) {
                $("#introduce .wasHide").css("display", "none");
                $("#introduce .head").removeClass("fixed");
                $("#introduce .head").css("margin-top", 0);
            }
        } else {//未展开
            if (nT <= 0) {
                $("#newest .wasHide").css("display", "block");
                $("#newest h2").addClass("fixed");
            } else {
                $("#newest .wasHide").css("display", "none");
                $("#newest h2").removeClass("fixed");
            }

        }

    },
    end:function(e){
        if (this.flag) {
            $(".lazy").lazyload({
                effect: "fadeIn"
            });
            if ($("#main-container").height() <= winH + (-$("#main-container").offset().top)) {
                pagesize++;
                _view.ajaxRequest();
            } else {
            }
        }
        this.flag = false;
    },
    clickIntroduce:function(){
        if ($("#introduce .introduce_content").height() > 0) {
            if ($("#introduce .head").hasClass("fixed")) {
                $("#main-container").offset({top: -$("#bigP").height()});
            }

        }
        $("#introduce .head").removeClass("fixed");
        $("#introduce .wasHide").css("display", "none");
        $("#introduce .introduce_content").toggle();
        $("#introduce .head span:nth-of-type(2)").toggleClass("open");
    },

};
var init=function(){
    _view.init();
    _event.binding();
};


$(document).ready(function () {
    index = location.href.lastIndexOf("/");
    str = location.href.substr(index + 1);
    type = str.split("_")[0];
    id = str.split("_")[1];
    pagesize = 1;
    $coverlist = $('.content-item').find('.lists');
    winH = document.documentElement.clientHeight;
    $word = $("#bigP .word");

    init();
});
